<?php if (! defined('APPPATH')) die('No direct script access allowed');
/**
 * RENARTI IK
 * An open source application development framework for PHP 5.2.0 or newer
 *
 * @package		RtiFramework
 * @author		Nauris Dambis <nauris.dambis@renarti.com>
 * @copyright	Copyright (c) 2012, RENARTI IK.
 * @license		http://renarti.com/#license
 * @link		http://renarti.com
 * @since		Version 1.0
 */

class RtiAgent
{
	protected $_agent = '';
	
	protected $_languages = array();
	
	
	/**
	 * Constructor
	 *
	 * Initialize the Agent string and variables
	 *
	 * @access	public
	 * @return	void
	 */
	public function __construct()
	{
		$this->_setAgent();
		$this->_setLanguages();
	}
	
	/**
	 * 
	 * Get $_SERVER['HTTP_USER_AGENT']
	 * 
	 * @access	public
	 * @return string
	 */
	public function userAgent()
	{
		return $this->_agent;
	}
	
	/**
	 * Is this a referral from another url/site?
	 *
	 * @access	public
	 * @return	bool
	 */
	public function isReferer()
	{
		if (! isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '')
		{
			return FALSE;
		}
		return TRUE;
	}
	
	/**
	 * Get the referrer
	 *
	 * @access	public
	 * @return	string
	 */
	public function referer()
	{
		return ( ! isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == '') ? '' : trim($_SERVER['HTTP_REFERER']);
	}
	
	/**
	 * Get the Agent LAnguages
	 *
	 * @access	public
	 * @return	array
	 */
	public function languages()
	{
		return $this->_languages;
	}
	
	private function _setAgent()
	{
		if (isset($_SERVER['HTTP_USER_AGENT']))
		{
			$this->_agent = trim($_SERVER['HTTP_USER_AGENT']);
		}		
	}
	
	private function _setLanguages()
	{
		if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && $_SERVER['HTTP_ACCEPT_LANGUAGE'] != '')
		{
			$this->_languages = preg_replace('#(;q=[0-9\.]+)#i', '', strtolower(trim($_SERVER['HTTP_ACCEPT_LANGUAGE'])));
		}

		if (empty($this->languages))
		{
			$this->languages[] = Rti::config('config')->get('LANGUAGE');
		}		
	}
	

}

/* End of file RtiAgent.php */
/* Location: ./system/core/RtiAgent.php */
